package com.fr.base.core;

import com.fr.base.ArrayUtils;
import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.core.util.ListMap;
import com.fr.util.OperatingSystem;
import com.fr.util.Utils;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.PixelGrabber;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;

public class BaseCoreUtils
{
  private static final char[] digits = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

  public static String convertIntToABC(int paramInt)
  {
    int i = paramInt;
    StringBuffer localStringBuffer = new StringBuffer();
    if (i == 0)
      return "";
    while (i != 0)
    {
      int j = i % 26;
      if (j == 0)
        j = 26;
      localStringBuffer.insert(0, digits[(j - 1)]);
      i = (i - j) / 26;
    }
    return localStringBuffer.toString();
  }

  public static int convertABCToInt(String paramString)
  {
    int i = 0;
    Character localCharacter = new Character('A');
    paramString = paramString.toUpperCase();
    int j = paramString.length();
    for (int k = 0; k < j; ++k)
      i = Character.getNumericValue(paramString.charAt(k)) - Character.getNumericValue(localCharacter.charValue()) + 26 * i + 1;
    return i;
  }

  public static String encodeString(String paramString, String[][] paramArrayOfString)
  {
    if (paramString == null)
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    for (int i = 0; i < paramString.length(); ++i)
    {
      char c = paramString.charAt(i);
      for (int j = 0; j < paramArrayOfString[1].length; ++j)
        if (c == paramArrayOfString[1][j].charAt(0))
        {
          localStringBuffer.append(paramArrayOfString[0][j]);
          break label86:
        }
      localStringBuffer.append(c);
    }
    label86: return localStringBuffer.toString();
  }

  public static double convertMillimetersToInches(double paramDouble)
  {
    return (paramDouble / 25.399999999999999D);
  }

  public static double convertInchesToMillimeters(double paramDouble)
  {
    BigDecimal localBigDecimal1 = new BigDecimal("" + paramDouble);
    BigDecimal localBigDecimal2 = new BigDecimal("25.4");
    return localBigDecimal1.multiply(localBigDecimal2).doubleValue();
  }

  public static double convertInchesToMillimeters2Show(double paramDouble)
  {
    double d = convertInchesToMillimeters(paramDouble);
    return (Math.floor(d * 10.0D) / 10.0D);
  }

  public static List getMacAddresses()
    throws IOException
  {
    Object localObject;
    ArrayList localArrayList = new ArrayList();
    String str1 = null;
    Process localProcess = null;
    BufferedReader localBufferedReader = null;
    String str2 = System.getProperty("os.name", "");
    if (str2.startsWith("Windows"))
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "ipconfig", "/all" }, null);
    }
    else if (str2.startsWith("AIX"))
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "netstat", "-v" }, null);
    }
    else if ((str2.startsWith("Solaris")) || (str2.startsWith("SunOS")))
    {
      localObject = getFirstLineOfCommand(new String[] { "uname", "-n" });
      if (localObject != null)
        localProcess = Runtime.getRuntime().exec(new String[] { "/usr/sbin/arp", localObject }, null);
    }
    else if (new File("/usr/sbin/lanscan").exists())
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "/usr/sbin/lanscan" }, null);
    }
    else if (new File("/sbin/ifconfig").exists())
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "/sbin/ifconfig", "-a" }, null);
    }
    if (localProcess != null)
    {
      localObject = localProcess.getInputStream();
      localBufferedReader = new BufferedReader(new InputStreamReader((InputStream)localObject), 128);
      String str3 = null;
      while (true)
      {
        do
        {
          if ((str3 = localBufferedReader.readLine()) == null)
            break label319;
          FRContext.getLogger().log(Level.INFO, str3);
          str1 = parseMacAddress(str3);
        }
        while ((str1 == null) || (parseShort(str1) == 255));
        localArrayList.add(str1);
      }
    }
    label319: return ((List)localArrayList);
  }

  public static String getMacAddress()
    throws IOException
  {
    return ((String)getMacAddresses().get(0));
  }

  public static boolean matchMacAddress(String paramString)
    throws IOException
  {
    String str1 = null;
    Process localProcess = null;
    BufferedReader localBufferedReader = null;
    String str2 = System.getProperty("os.name", "");
    if (OperatingSystem.isWindows())
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "ipconfig", "/all" }, null);
    }
    else if (str2.startsWith("AIX"))
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "netstat", "-v" }, null);
    }
    else if ((str2.startsWith("Solaris")) || (str2.startsWith("SunOS")))
    {
      localObject = getFirstLineOfCommand(new String[] { "uname", "-n" });
      if (localObject != null)
        localProcess = Runtime.getRuntime().exec(new String[] { "/usr/sbin/arp", localObject }, null);
    }
    else if (new File("/usr/sbin/lanscan").exists())
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "/usr/sbin/lanscan" }, null);
    }
    else if (new File("/sbin/ifconfig").exists())
    {
      localProcess = Runtime.getRuntime().exec(new String[] { "/sbin/ifconfig", "-a" }, null);
    }
    Object localObject = paramString.split(",");
    if (localProcess != null)
    {
      localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream()), 128);
      String str3 = null;
      do
      {
        if ((str3 = localBufferedReader.readLine()) == null)
          break label301;
        str1 = parseMacAddress(str3);
      }
      while ((str1 == null) || (parseShort(str1) == 255) || (ArrayUtils.indexOf(localObject, str1) == -1));
      return true;
    }
    label301: return false;
  }

  private static short parseShort(String paramString)
    throws NullPointerException
  {
    paramString = paramString.toLowerCase();
    int i = 0;
    int j = 0;
    for (int l = 0; (l < paramString.length()) && (j < 4); ++l)
    {
      int k = paramString.charAt(l);
      if ((k > 47) && (k < 58))
      {
        i = (short)(i << 4);
        j = (byte)(j + 1);
        i = (short)(i | k - 48);
      }
      else if ((k > 96) && (k < 103))
      {
        j = (byte)(j + 1);
        i = (short)(i << 4);
        i = (short)(i | k - 87);
      }
    }
    return i;
  }

  private static String getFirstLineOfCommand(String[] paramArrayOfString)
    throws IOException
  {
    Process localProcess = null;
    BufferedReader localBufferedReader = null;
    try
    {
      localProcess = Runtime.getRuntime().exec(paramArrayOfString);
      localBufferedReader = new BufferedReader(new InputStreamReader(localProcess.getInputStream()), 128);
      String str = localBufferedReader.readLine();
      return str;
    }
    finally
    {
      if (localProcess != null)
      {
        if (localBufferedReader != null)
          try
          {
            localBufferedReader.close();
          }
          catch (IOException localIOException1)
          {
          }
        try
        {
          localProcess.getErrorStream().close();
        }
        catch (IOException localIOException2)
        {
        }
        try
        {
          localProcess.getOutputStream().close();
        }
        catch (IOException localIOException3)
        {
        }
        localProcess.destroy();
      }
    }
  }

  private static String parseMacAddress(String paramString)
  {
    int i = paramString.indexOf("0x");
    if ((i != -1) && (paramString.indexOf("ETHER") != -1))
    {
      j = paramString.indexOf(32, i);
      if (j > i + 2)
        return paramString.substring(i, j);
    }
    int j = 0;
    if (paramString.indexOf(45) > -1)
      paramString = paramString.replace('-', ':');
    int k = paramString.lastIndexOf(58);
    if (k > paramString.length() - 2)
      return null;
    int i1 = Math.min(paramString.length(), k + 3);
    ++j;
    int l = k;
    while (true)
    {
      do
      {
        if ((j == 5) || (k == -1) || (k <= 1))
          break label153;
        k = paramString.lastIndexOf(58, --k);
      }
      while ((l - k != 3) && (l - k != 2));
      ++j;
      l = k;
    }
    if ((j == 5) && (k > 1))
      label153: return paramString.substring(k - 2, i1).trim();
    return null;
  }

  public static BufferedImage toBufferedImage(Image paramImage)
  {
    if (paramImage instanceof BufferedImage)
      return ((BufferedImage)paramImage);
    paramImage = new ImageIcon(paramImage).getImage();
    boolean bool = hasAlpha(paramImage);
    BufferedImage localBufferedImage = null;
    GraphicsEnvironment localGraphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try
    {
      int i = 1;
      if (bool)
        i = 2;
      GraphicsDevice localGraphicsDevice = localGraphicsEnvironment.getDefaultScreenDevice();
      GraphicsConfiguration localGraphicsConfiguration = localGraphicsDevice.getDefaultConfiguration();
      localBufferedImage = localGraphicsConfiguration.createCompatibleImage(paramImage.getWidth(null), paramImage.getHeight(null), i);
    }
    catch (HeadlessException localHeadlessException)
    {
      FRContext.getLogger().log(Level.WARNING, localHeadlessException.getMessage());
    }
    if (localBufferedImage == null)
    {
      int j = 1;
      if (bool)
        j = 2;
      localBufferedImage = GraphHelper.createBufferedImage(paramImage.getWidth(null), paramImage.getHeight(null), j);
    }
    Graphics2D localGraphics2D = localBufferedImage.createGraphics();
    localGraphics2D.drawImage(paramImage, 0, 0, null);
    localGraphics2D.dispose();
    return localBufferedImage;
  }

  private static boolean hasAlpha(Image paramImage)
  {
    if (paramImage instanceof BufferedImage)
    {
      localObject = (BufferedImage)paramImage;
      return ((BufferedImage)localObject).getColorModel().hasAlpha();
    }
    Object localObject = new PixelGrabber(paramImage, 0, 0, 1, 1, false);
    try
    {
      ((PixelGrabber)localObject).grabPixels();
    }
    catch (InterruptedException localInterruptedException)
    {
      FRContext.getLogger().log(Level.WARNING, localInterruptedException.getMessage());
    }
    ColorModel localColorModel = ((PixelGrabber)localObject).getColorModel();
    return localColorModel.hasAlpha();
  }

  public static boolean checkDesingerActive(String paramString)
  {
    return ((paramString != null) && (paramString.length() == 26) && (paramString.charAt(5) == 'A') && (paramString.charAt(15) == 'F'));
  }

  public static String writeESC(String paramString)
  {
    if (paramString == null)
      return "";
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    int j = paramString.length();
    while (i < j)
    {
      char c;
      switch (c = paramString.charAt(i))
      {
      case '\n':
        localStringBuffer.append("\\n");
        break;
      case '\\':
        localStringBuffer.append("\\\\");
        break;
      case '\t':
        localStringBuffer.append("\\t");
        break;
      case '\r':
        localStringBuffer.append("\\r");
        break;
      case '\b':
        localStringBuffer.append("\\b");
        break;
      case '\f':
        localStringBuffer.append("\\f");
        break;
      case '\'':
        localStringBuffer.append("\\'");
        break;
      case '"':
        localStringBuffer.append("\\\"");
        break;
      default:
        localStringBuffer.append(c);
      }
      ++i;
    }
    return localStringBuffer.toString();
  }

  public static String readESC(String paramString)
    throws Exception
  {
    if (paramString == null)
      return "";
    Pattern localPattern = Pattern.compile("\\\\");
    Matcher localMatcher = localPattern.matcher(paramString);
    int i = 0;
    int j = 0;
    StringBuffer localStringBuffer = new StringBuffer();
    while (localMatcher.find(i))
    {
      i = localMatcher.start();
      localStringBuffer.append(paramString.substring(j, i));
      j = localMatcher.end();
      String str1 = paramString.substring(j);
      if (str1.length() == 0)
        throw new Exception("\\ can't be parsed.");
      int k = str1.charAt(0);
      switch (k)
      {
      case 110:
        localStringBuffer.append('\n');
        ++j;
        break;
      case 114:
        localStringBuffer.append('\r');
        ++j;
        break;
      case 116:
        localStringBuffer.append('\t');
        ++j;
        break;
      case 98:
        localStringBuffer.append('\b');
        ++j;
        break;
      case 102:
        localStringBuffer.append('\f');
        ++j;
        break;
      case 34:
        localStringBuffer.append('"');
        ++j;
        break;
      case 39:
        localStringBuffer.append('\'');
        ++j;
        break;
      case 92:
        localStringBuffer.append('\\');
        ++j;
        break;
      case 117:
        String str2 = paramString.substring(j);
        if (str2.length() < 4)
          throw new Exception("\\u" + str2 + " can't be parsed.");
        localStringBuffer.append(parseHex(str2.substring(0, 4)));
        j += 5;
        break;
      default:
        int l = 0;
        if (str1.matches("[0-3][0-7][0-7].*"))
          l = 3;
        else if (str1.matches("[0-7][0-7].*"))
          l = 2;
        else if (str1.matches("[0-7].*"))
          l = 1;
        if (l > 0)
        {
          localStringBuffer.append(parseOctal(paramString.substring(j, j + l)));
          j += l;
        }
        else
        {
          throw new Exception("\\" + str1 + " can't be parsed.");
        }
      }
      i = j;
    }
    localStringBuffer.append(paramString.substring(j));
    return localStringBuffer.toString();
  }

  private static char parseHex(String paramString)
  {
    return (char)Integer.decode("0x" + paramString).intValue();
  }

  private static char parseOctal(String paramString)
  {
    return (char)Integer.decode('0' + paramString).intValue();
  }

  public static String[] pathSplit(String paramString)
  {
    return paramString.split("[/\\\\]");
  }

  public static String pathJoin(String[] paramArrayOfString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = 0;
    int j = (paramArrayOfString == null) ? 0 : paramArrayOfString.length;
    while (i < j)
    {
      String str = paramArrayOfString[i];
      if (str == null)
        str = "";
      if ((i > 0) && (((str.startsWith("/")) || (str.startsWith("\\")))))
        str = str.substring(1);
      localStringBuffer.append(str);
      if ((i + 1 < j) && (!(str.endsWith("/"))) && (!(str.endsWith("\\"))))
        localStringBuffer.append("/");
      ++i;
    }
    return localStringBuffer.toString();
  }

  public static String getRelativePath(File paramFile1, File paramFile2)
  {
    String str1 = paramFile1.getAbsolutePath();
    String str2 = paramFile2.getAbsolutePath();
    if (str1.length() < str2.length())
      return null;
    if (str1.equals(str2))
      return "";
    if (str1.startsWith(str2))
    {
      String str3 = str1.substring(str2.length());
      if ((!(str3.startsWith("/"))) && (!(str3.startsWith("\\"))))
        str3 = File.separator + str3;
      return str3;
    }
    return null;
  }

  public static String join(Collection paramCollection, String paramString)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    Iterator localIterator = paramCollection.iterator();
    while (true)
    {
      do
      {
        if (!(localIterator.hasNext()))
          break label57;
        Object localObject = localIterator.next();
        localStringBuffer.append(localObject);
      }
      while (!(localIterator.hasNext()));
      localStringBuffer.append(paramString);
    }
    label57: return localStringBuffer.toString();
  }

  public static String join(Collection paramCollection)
  {
    return join(paramCollection, "");
  }

  public static String join(Object[] paramArrayOfObject, String paramString)
  {
    return join(Arrays.asList(paramArrayOfObject), paramString);
  }

  public static String join(Object[] paramArrayOfObject)
  {
    return join(paramArrayOfObject, "");
  }

  public static List map(List paramList, Mapper paramMapper)
  {
    int i = paramList.size();
    Object[] arrayOfObject = new Object[i];
    for (int j = 0; j < i; ++j)
      arrayOfObject[j] = paramMapper.map(j, paramList.get(j), paramList);
    return Arrays.asList(arrayOfObject);
  }

  public static Object cloneObject(Object paramObject)
    throws CloneNotSupportedException
  {
    if (paramObject instanceof FCloneable)
      return ((FCloneable)paramObject).clone();
    if (paramObject instanceof Cloneable)
    {
      Method localMethod = Utils.getDeclaredMethod(paramObject.getClass(), "clone", new Class[0]);
      localMethod.setAccessible(true);
      try
      {
        return localMethod.invoke(paramObject, new Object[0]);
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    }
    return paramObject;
  }

  public static Map cloneMap(Map paramMap)
    throws CloneNotSupportedException
  {
    if (paramMap == null)
      return null;
    if (paramMap instanceof HashMap)
      return ((Map)((HashMap)paramMap).clone());
    if (paramMap instanceof Hashtable)
      return ((Map)((Hashtable)paramMap).clone());
    if (paramMap instanceof ListMap)
      return ((Map)((ListMap)paramMap).clone());
    try
    {
      Method localMethod = Utils.getDeclaredMethod(paramMap.getClass(), "clone", new Class[0]);
      localMethod.setAccessible(true);
      return ((Map)localMethod.invoke(paramMap, new Object[0]));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return paramMap;
  }
}